import Router from '@koa/router';
const router = new Router({ prefix: '/print' });

router.post('/', async (ctx) => {
    try {
        let files = (ctx.request.body as { files: unknown }).files;
        if (files && typeof files === 'string') files = JSON.parse(files);
        if (!Array.isArray(files) || !files.length) throw Error('参数不合法');

        process.parentPort.postMessage({
            type: 'print:file',
            value: files
        });

        ctx.body = {
            code: 200,
            result: true,
            message: '打印任务发送成功!'
        };
    } catch (e) {
        ctx.body = {
            code: 400,
            result: false,
            message: `打印失败:${(e as Error).message}`
        };
    }
});

export default router;
